package com.example.myapplication;


import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button addBtn,deleteBtn,modifyBtn,searchBtn,createBtn;
    private TextView outText;
    EditText userEdit,pwdEdit;
    SQLiteDatabase db;
    MyHelper myHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

    }



    private void initView(){
        addBtn=findViewById(R.id.add);
        addBtn.setOnClickListener(this);
        deleteBtn=findViewById(R.id.delete);
        deleteBtn.setOnClickListener(this);
        modifyBtn=findViewById(R.id.modify);
        modifyBtn.setOnClickListener(this);
        searchBtn=findViewById(R.id.search);
        searchBtn.setOnClickListener(this);
        createBtn=findViewById(R.id.createBase);
        createBtn.setOnClickListener(this);
        outText=findViewById(R.id.outText);
        userEdit=findViewById(R.id.username);
        pwdEdit=findViewById(R.id.password);
    }

    @Override
    public void onClick(View view) {
        switch(view.getId())
        {
            case R.id.createBase:
                myHelper=new MyHelper(MainActivity.this,"MyDataBase.db",null,666);
                //创建一个可读可写的数据库
                db=myHelper.getWritableDatabase();
//                //获取一个只读数据库
//                SQLiteDatabase db1=myHelper.getReadableDatabase();
                break;
            case R.id.add:
                String username=userEdit.getText().toString();
                String password=pwdEdit.getText().toString();
                //创建一个contentValues对象，用于记录存储的字段值,以键值对方式存储，键对应的就是字段名，值就是某一字段具体的值
                ContentValues contentValues=new ContentValues();
                contentValues.put("userName",username);
                contentValues.put("password",password);
                db.insert("user",null,contentValues);
                db.close();
                break;
            case R.id.delete:

                break;
            case R.id.modify:

                break;
            case R.id.search:

                break;
        }
    }

    class MyHelper extends SQLiteOpenHelper{
        //第一个参数是上下文，第二个叫数据库名称，第三个结果集工厂，第四个版本号
        public MyHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
        }
        //数据库创建时用于造表或造视图文件
        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
            sqLiteDatabase.execSQL("create table user(userID integer primary key autoincrement,userName varchar(10),password varchar(10))");
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

        }
    }
}